package com.qyhl.webtv.module_live.teletext.mixlive;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.glide.GlideRoundTransform;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.live.MixListBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_live.R;
import com.qyhl.webtv.module_live.R2;
import com.qyhl.webtv.module_live.teletext.sandulive.SDLiveFragment;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.analytics.MobclickAgent;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

/**
 * Created by qyhl on 2018/1/30.
 */

public class MixLiveListFragment extends BaseFragment implements MixLiveListContract.MixLiveView {
    @BindView(R2.id.title)
    TextView mTitle;
    @BindView(R2.id.listview)
    ListView listview;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    private View view;
    private MixLiveListPresenter mPresenter;
    @BindView(R2.id.barlayout)
    RelativeLayout barlayout;
    private String title;
    private String type;
    private String newsId;

    private List<MixListBean> mDataList;

    private CommonAdapter mAdapter;

    public void setTitle(String title) {
        this.title = title;
    }

    public void setType(String type) {
        this.type = type;
    }


    public static MixLiveListFragment newInstance(String title, String type) {
        MixLiveListFragment fragment = new MixLiveListFragment();
        fragment.setTitle(title);
        fragment.setType(type);
        return fragment;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view = inflater.inflate(R.layout.live_fragment_mixlive_list, null);
    }

    @Override
    protected void create() {
        mPresenter = new MixLiveListPresenter(this);

        initView();
        setListener();
    }

    @Override
    public void initImmersionBar() {
        if (StringUtils.isNotEmpty(title)) {
            ImmersionBar.with(this)
                    .fitsSystemWindows(true)
                    .statusBarDarkFont(true)
                    .statusBarColor(R.color.white)
                    .init();
        }

    }

    @Override
    protected void onFirstVisible() {
        loadData();
    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }


    /**
     * 加载数据
     */
    private void loadData() {
        mPresenter.getList(newsId, type);
    }

    /**
     * 初始化
     */
    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);

        mDataList = new ArrayList<>();

        newsId = "0";
        if (StringUtils.isEmpty(type)) {
            type = "";
        }

        if (TextUtils.isEmpty(title)) {
            barlayout.setVisibility(View.GONE);
        } else {
            mTitle.setText(title);
        }


        refresh.setEnableLoadMore(true);
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));
        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setDisableContentWhenRefresh(true);
        refresh.setDisableContentWhenLoading(true);

        listview.setAdapter(mAdapter = new CommonAdapter<MixListBean>(getContext(), R.layout.live_item_mixlive_list, mDataList) {
            @Override
            protected void convert(ViewHolder viewHolder, MixListBean item, int position) {
                ImageView cover = viewHolder.getView(R.id.cover);
                Glide.with(getContext())
                        .load(item.getCoverImg())
                        .apply(new RequestOptions().error(R.drawable.cover_normal_default).placeholder(R.drawable.cover_normal_default).transform(new GlideRoundTransform(4)))
                        .into(cover);

                TextView title = viewHolder.getView(R.id.title);
                title.setText(item.getName());

                TextView scanNum = viewHolder.getView(R.id.scan_num);
                if (item.getType() == 1) {
                    scanNum.setText(StringUtils.transformNum(item.getHits() + "") + "人看过");
                } else {
                    scanNum.setText(StringUtils.transformNum(item.getHits() + "") + "人听过");
                }
            }
        });

    }

    /**
     * 监听
     */
    @Override
    protected void setListener() {
        //重载
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                newsId = "0";
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getList(newsId, type);
            }
        });
        //加载
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                mPresenter.getList(newsId, type);
            }
        });
        //刷新
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                newsId = "0";
                mPresenter.getList(newsId, type);
            }
        });

        listview.setOnItemClickListener((parent, view, position, id) -> {
            if (mDataList.get(position).getType() == 1) {

                Bundle bundle = new Bundle();
                bundle.putString("id", mDataList.get(position).getId() + "");
                RouterManager.router(ARouterPathConstant.LIVE_VIDEO, bundle);

            } else {
                Bundle bundle = new Bundle();
                bundle.putString("id", mDataList.get(position).getId() + "");
                RouterManager.router(ARouterPathConstant.LIVE_RADIO, bundle);
            }
        });


    }


    /**
     * 设置数据
     *
     * @param beanList
     * @param isMore
     */
    @Override
    public void setList(List<MixListBean> beanList, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~~");
        loadMask.setStatus(LoadingLayout.Success);
        newsId = beanList.get(beanList.size() - 1).getId() + "";
        if (isMore) {
            refresh.finishLoadMore();
            mDataList.addAll(beanList);
        } else {
            refresh.finishRefresh();
            mDataList.clear();
            mDataList.addAll(beanList);
        }

        mAdapter.notifyDataSetChanged();
    }

    /**
     * 网络出错
     *
     * @param msg
     */
    @Override
    public void setNetError(String msg) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.No_Network);
        loadMask.setNoNetworkText(msg);
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * 无内容
     *
     * @param msg
     */
    @Override
    public void setEmpty(String msg) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Empty);
        loadMask.setEmptyText(msg);
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * 出错
     *
     * @param msg
     */
    @Override
    public void setError(String msg) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setErrorText(msg);
        loadMask.setReloadButtonText("点击重试~~");
    }

    @Override
    public void showLog(String msg) {
        Log.i("info", msg);
        refresh.finishLoadMore();
    }

    @Override
    public void showMessage(String msg) {
        Toasty.normal(getContext(), msg).show();
        refresh.finishLoadMore();
    }

    @Override
    public void onResume() {
        super.onResume();
        //umeng
        MobclickAgent.onPageStart("混合音视频列表");
        MobclickAgent.onResume(getContext());
    }

    @Override
    public void onPause() {
        super.onPause();
        //umeng
        MobclickAgent.onPageEnd("混合音视频列表");
        MobclickAgent.onPause(getContext());
    }

}
